package cn.tao.utils;

import cn.tao.org.mapper.EmployeeMapper;
import cn.tao.user.domain.LoginInfo;
import cn.tao.user.domain.User;
import cn.tao.user.mapper.UserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {

    //返回两种 要么user  要么employee
    public static Object getCurrentUser(HttpServletRequest request){

        //获取请求头，从而拿到登录用户信息
        String header = request.getHeader("U-TOKEN");
        //获取spring的容器
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        //从容器中拿到指定的对象
        RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        //从redis中取出登录信息
        LoginInfo loginInfo = (LoginInfo)redisTemplate.opsForValue().get(header);
        if (loginInfo.getType()==0){//后台用户
            EmployeeMapper employeeMapper = applicationContext.getBean(EmployeeMapper.class);
            return employeeMapper.loadByLoginInfoId(loginInfo.getId());

        }else {//前台用户
            UserMapper userMapper = applicationContext.getBean(UserMapper.class);
            //根据logininfoid查询user对象，返回一个user对象
            return  userMapper.loadByLoginInfoId(loginInfo.getId());
        }

    }

}
